package org.example.studyonline.controller;

import jakarta.annotation.Resource;
import org.example.studyonline.pojo.PageBean;
import org.example.studyonline.pojo.Result;
import org.example.studyonline.pojo.VO.CourseListVO;
import org.example.studyonline.pojo.VO.CourseVO;
import org.example.studyonline.service.CourseService;
import org.example.studyonline.service.VideoWatchRecordSerrvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Resource
    private VideoWatchRecordSerrvice videoWatchRecordSerrvice;

    //获取课程列表
    @GetMapping("/list")
    public Result<PageBean<CourseListVO>> getCourseList(@RequestParam(value = "page", defaultValue = "1") Long page, @RequestParam(value = "pageSize", defaultValue = "2") Long size){

        PageBean<CourseListVO> list = new PageBean<>();
        list =courseService.getCourseList(page,size);
        if (list == null) {
            return Result.error("获取课程列表失败");
        }
        return Result.success("获取课程列表成功",list);

    }

    // 获取课程详细信息
    @GetMapping("/{courseID}/chapters")
    public Result<CourseVO> getChapters(@PathVariable Integer courseID){
        // 校验 materialID 参数
        if (courseID == null || courseID <= 0) {
            return Result.error("Invalid materialID");
        }
        return Result.success("获取课程详细信息成功",courseService.getChapters(courseID));
    }

    // 观看视频
    @GetMapping("/watch/{materialID}")
    public Result watch(@PathVariable Integer materialID){
        // 校验 materialID 参数
        if (materialID == null || materialID <= 0) {
            return Result.error("Invalid materialID");
        }
        videoWatchRecordSerrvice.watch(materialID);
        return Result.success("已添加播放记录",null);
    }

    // 搜索课程

    @GetMapping("/search")
    public Result searchCourse(@RequestParam("keyword")String keyword,@RequestParam(value = "page", defaultValue = "1") Long pageNo, @RequestParam(value = "pageSize", defaultValue = "2") Long pageSize){
        return Result.success("获取课程列表成功",courseService.searchCourse(keyword,pageNo,pageSize));
    }
}
